﻿using LiteNetLib;
using LiteNetLib.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace Core
{
    internal class UdpSession : Session
    {
        public NetPeer peer;
        public NetManager manager;
        NetDataWriter dataWriter = new NetDataWriter();

        public UdpSession(NetPeer peer, NetManager manager)
        {
            this.peer = peer;
            this.manager = manager;
        }

        public override void Disconnect()
        {
            manager.Stop();
        }

        public override void SendMessage(IMessage message)
        {
            SendMessage(message, 0);
        }

        public override void SendMessage(IMessage message, byte rpcID)
        {
            var serializeData = new SerializeMessage(message, SessionID, rpcID, false).Publish();

            dataWriter.Reset();
            dataWriter.Put(serializeData.messageBuffer.buffer, 0, serializeData.messageBuffer.messageLength);
            MessageBuffer.Push(serializeData.messageBuffer);

            peer.Send(dataWriter, DeliveryMethod.ReliableOrdered);
        }

        public override bool Register(uint sessionID)
        {
            SessionID = sessionID;
            return true;
        }
    }
}
